//
// Created by zhengjuping on 24-9-12.
//

#include <esp_log.h>
#include "EnvIIGattCharacteristicDesc.h"

#define TAG "EnvIIGattCharacteristicDesc"

static const uint8_t defaultEnvValue[2] = {0x00, 0x00};


EnvIIGattCharacteristicDesc::EnvIIGattCharacteristicDesc() : GattCharacteristic(
        BleUuid(BleUuid::Width::UUID_16, 0x2902),
        defaultEnvValue,
        sizeof(defaultEnvValue),
        GattCharacteristic::ResponseMethod::RSP_BY_APP,
        GattCharacteristic::Properties::Read_Write_Notify,
        GattCharacteristic::Permissions::Read_Write) {

}





void EnvIIGattCharacteristicDesc::onWriteEvent(const uint8_t *data, uint16_t length) {
    if( !_observer.expired() ){
        bool notifyEnabled = false;

        uint16_t u16Data = data[0] | (data[1] << 8);

        if( u16Data == 1 ){
            notifyEnabled = true;
        }else{
            notifyEnabled = false;
        }
        ESP_LOGE( TAG, "notifyEnabled: %d, notifyEnabled:%d", u16Data, notifyEnabled );
        _observer.lock()->onNotify(notifyEnabled);
    }
}

void EnvIIGattCharacteristicDesc::setNotifyObserver(const std::shared_ptr<ICharacteristicNotifyObserver>& observer) {
    _observer = observer;
}

